package com.heima.behavior.service.iml;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    ApBehaviorEntryServiceImpl apBehaviorEntryService;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public ResponseResult loadApArticle(ArticleBehaviorDTO dto) {
        Map<String,Boolean> map = new HashMap<>();
        //1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user == null){//2. 未登录 直接将 4种行为 均返回false
            map.put("isfollow",false);
            map.put("islike",false);
            map.put("isunlike",false);
            map.put("iscollection",false);
            return ResponseResult.okResult(map);
        }
        // 3. 如果登录
        if (dto ==null){
            CustException.cust(AppHttpCodeEnum.PARAM_REQUIRE,"参数为空");
        }
        //4. 查询行为实体
        // 2根据当前登录用户id查询行为实体对象
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
        if (apBehaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"没有对应行为实体");
        }
        //3.1获取文章id
        Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));
        ApLikesBehavior one = mongoTemplate.findOne(query, ApLikesBehavior.class);
        ApUnlikesBehavior one1 = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        ApCollection one2 = mongoTemplate.findOne(query, ApCollection.class);
        if (one != null){
            map.put("islike",true);
        }else {
            map.put("islike",false);
        }
        if (one1 != null){
             map.put("isunlike",true);
        }else {
            map.put("isunlike",false);
        }
        if (one2 != null){
            map.put("iscollection",true);
        }else {
            map.put("iscollection",false);
        }
        //关注作者
        Double score = redisTemplate.opsForZSet().score("apuser:follow:", dto.getAuthorApUserId().toString());
        if (score == null){
            map.put("isfollow",false);
        }else {
            map.put("isfollow",true);
        }
        return ResponseResult.okResult(map);
    }
}
